import {
    GetOrderList,
    Delivery,
    GetStoreExpressList,
    GetSendAddrList,
    GetOrderPrintList,
    PrintedInvoice,
    GetPrintItemConfig,
    GetOrderDeliverList,
    DeliveryBatch,
    ImportLog,
    ImportExpress,
    PrintedCourierBill,
    GetPrintedCourierBill,
    GetPackageList,  
    EditPackage  
} from '@/services/order/deliverManage';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'deliverManage',

    state: {
        orderList: [],
        expressList:[],
        sendAddrList:[],
        orderDeliverList:[],
        printItemConfig:[],
        pendingDeliverList:[],
        importLogList:[],
        PrintedCourierBillList:[],
        packageList:[]
    },

    effects: {

        // 获取订单列表
        *GetOrderList({payload, callback}, {call, put}) {
            const data = yield call(GetOrderList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetOrderList',
                        payload: data.Data.DataList||[],
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        // 获取订单包裹列表
        *GetPackageList({payload, callback}, {call, put}) {
            const data = yield call(GetPackageList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetPackageList',
                        payload: data.Data.DataList||[],
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        // 获取批量发货记录列表
        *ImportLog({payload, callback}, {call, put}) {
        const data = yield call(ImportLog, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                yield put({
                    type: 'saveImportLog',
                    payload: data.Data.DataList||[],
                });
                callback && callback.success&&callback.success(data);
            } else {
                callback && callback.error&&callback.error(data)
            }
        })         
    },

        // 单个发货
        *Delivery({payload, callback}, {call, put}) {
            const data = yield call(Delivery, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
          },        
          // 获取上一次打印快递单的记录
          *GetPrintedCourierBill({payload, callback}, {call, put}) {
            const data = yield call(GetPrintedCourierBill, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetPrintedCourierBill',
                        payload: data.Data||[],
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
          },
          *GetStoreExpressList({payload, callback}, {call, put}) {
            const data = yield call(GetStoreExpressList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetStoreExpressList',
                        payload: data.Data.DataList||[],
                    });
                        callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
          },
        // 获取发货地址列表
        *GetSendAddrList({payload, callback}, {call, put}) {
            const data = yield call(GetSendAddrList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveSendAddrList',
                        payload: data.Data.DataList||[],
                    });
                        callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
          },
        *GetOrderPrintList({payload, callback}, {call, put}) {
            const data = yield call(GetOrderPrintList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveOrderDeliverList',
                        payload: data.Data||[],
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        *PrintedInvoice({payload, callback}, {call, put}) {
            const data = yield call(PrintedInvoice, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {                
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        *PrintedCourierBill({payload, callback}, {call, put}) {
            const data = yield call(PrintedCourierBill, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {                
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },        
        *GetPrintItemConfig({payload, callback}, {call, put}) {
            const data = yield call(GetPrintItemConfig, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) { 
                    yield put({
                        type: 'savePrintItemConfig',
                        payload: data.Data||[],
                    });               
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        *GetOrderDeliverList({payload, callback}, {call, put}) {
            const data = yield call(GetOrderDeliverList, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'savePendingDeliverList',
                        payload: data.Data||[],
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        *DeliveryBatch({payload, callback}, {call, put}) {
            const data = yield call(DeliveryBatch, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {                
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },

        //导入快递单
        *ImportExpress({payload, callback}, {call, put}) {
            const data = yield call(ImportExpress, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {                
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })              
        },
        // 修改物流
        *EditPackage({payload, callback}, {call, put}) {
            const data = yield call(EditPackage, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {                
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })             
        },
    },

    reducers: {
        saveGetOrderList(state, {payload}) {
            return {
                ...state,
                orderList: payload,
            };
        },
        saveStoreExpressList(state, {payload}) {
            return {
                ...state,
                expressList: payload,
            };
        },
        saveSendAddrList(state, {payload}) {
            return {
                ...state,
                sendAddrList: payload,
            };
        },
        saveOrderDeliverList(state, {payload}) {
            return {
                ...state,
                orderDeliverList: payload,
            };
        },
        savePrintItemConfig(state, {payload}) {
            return {
                ...state,
                printItemConfig: payload,
            };
        },
        savePendingDeliverList(state, {payload}) {
            return {
                ...state,
                pendingDeliverList: payload,
            };
        },
        saveImportLog(state, {payload}) {
            return {
                ...state,
                importLogList: payload,
            };
        },
        saveGetPrintedCourierBill(state, {payload}) {
            return {
                ...state,
                PrintedCourierBillList: payload,
            };
        },
        saveGetPackageList(state, {payload}) {
            return {
                ...state,
                packageList: payload,
            };
        },
        saveGetStoreExpressList(state, {payload}) {
            return {
                ...state,
                expressList: payload,
            };
        }
    }
}
